package net.zjitc.controller;

import net.zjitc.pojo.Cart;
import net.zjitc.pojo.Orders;
import net.zjitc.pojo.User;
import net.zjitc.service.CartService;
import net.zjitc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/17 14:55
 */
@Controller
@RequestMapping("/order")
public class UserOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;

    /**
     * 创建生成订单
     * @return
     *
     * 这里生成的订单是将当前用户下购物车中的所有商品生成订单
     * 实际（选择购物车条目，生成订单）
     */
    @RequestMapping({"/createOrder","/paymentOrder"})
    public String createOrder(HttpSession session, Model model,Orders orders){
        if (orders.getId()==null){
        User user = (User) session.getAttribute("user");
        int resultCount = orderService.createOrder(orders,user);
        }
        model.addAttribute("order",orders);
        return "/WEB-INF/alipay/alipay.trade.page.pay.jsp";
    }

    @RequestMapping("/myOrders")
    public String myOrders(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Orders> ordersList = orderService.findOrdersByUid(user.getId());
        model.addAttribute("orderList",ordersList);
        return "/client/orderlist.jsp";

    }

    @RequestMapping("/orderDetails")
    public String showOrderDetails(Model model,String id){
        Orders orderDeatils = orderService.findOrderDeatils(id);
        model.addAttribute("order",orderDeatils);
        return "/client/orderInfo.jsp";
    }

    //进入结算页面，显示选中的购物车中的商品及收货人信息
    @RequestMapping("/showReceiveInfo")
    public String showReceiveInfo(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Cart> cartList = cartService.findCartsByUid(user.getId());
        model.addAttribute("cartList",cartList);
        return "/client/order.jsp";

    }

  /*  *//*订单在未支付的状态下，进行支付*//*
    @RequestMapping("/paymentOrder")
    public String paymentOrder(Orders orders,Model model){
        model.addAttribute("order",orders);
        return "/WEB-INF/alipay/alipay.trade.page.pay.jsp";
    }
*/
    @RequestMapping("/payReturn")
    public String payReturn(Model model,String out_trade_no){
       int resultCount =  orderService.updatePayState(out_trade_no,1);
        model.addAttribute("msg","付款成功、、、");
        return "/client/createOrderSuccess.jsp";
    }

}
